<script setup lang="ts">
import { ref } from 'vue'
import { onShow } from '@dcloudio/uni-app'
import { getMedicalDeptList, getMedicalDeptSubList } from '@/services/medicalDept'
import type { baseMedicalDept, baseMedicalDeptSub } from '@/types/medicalDept'

//当前选中的科室id
const deptId = ref<number | null>(null)
// 科室列表
const deptList = ref<baseMedicalDept[]>([])
//处理点击科室item事件
const clickDeptHandle = (id: number) => {
  deptId.value = id
  searchMedicalDeptSubList(id)
}
// 查询科室列表
const searchMedicalDeptList = async () => {
  await getMedicalDeptList({ outpatient: true }).then((res) => {
    deptList.value = res.result
    deptId.value = deptList.value[0].id
    searchMedicalDeptSubList(deptList.value[0].id)
  })
}

// 诊室列表
const sub = ref<baseMedicalDeptSub[]>([])
//处理点击诊室item事件
const clickSubHandle = (deptSubId: number, deptSubName: string) => {
  uni.navigateTo({
    url: `/registration/dept_sub_plan/dept_sub_plan?deptSubId=${deptSubId}&deptSubName=${deptSubName}`,
  })
}
// 查询诊室列表
const searchMedicalDeptSubList = async (deptId: number) => {
  await getMedicalDeptSubList({ deptId }).then((res) => {
    sub.value = res.result
  })
}

onShow(() => {
  searchMedicalDeptList()
})
</script>

<template>
  <view>
    <view class="main">
      <view class="left">
        <view
          :class="one.id == deptId ? 'item active' : 'item'"
          v-for="one in deptList"
          :key="one.id"
          @tap="clickDeptHandle(one.id)"
        >
          {{ one.name }}
        </view>
      </view>
      <view class="right">
        <view
          class="item"
          v-for="one in sub"
          @tap="clickSubHandle(one.id, one.name)"
          :key="one.id"
          >{{ one.name }}</view
        >
      </view>
    </view>
  </view>
</template>

<style lang="less">
@import url(medical_dept_list.less);
</style>
